#!/bin/bash

. vender/scripts/get_env.sh --source-only

get_build_env;

usage()
{
	echo ""
	echo "Usage: `basename $0` [options] [-jn] [-v] [-h]"
	echo "Options:"
	echo "  release, debug: "
	echo "      Specifies the build type."
	echo "  i386, amd64, arm, arm64, mips, mips64, ppc, ppc64:"
	echo "      Specifies the architecture for code generation."
	echo "  clean: "
	echo "      Clean the build folder."
	echo "  ci: "
	echo "      Specifies the environment is CI."
	echo "  -h, --help:"
	echo "      Print this message and exit."
	echo "  -j: enable make '-j' option."
	echo "      if 'n' is not given, will set jobs to auto detected core count, otherwise n is used."
	echo "  -v: verbose make"
	echo ""
	exit 0
}

for i in "$@"
do
	case $i in
		i386|amd64|arm|arm64|mips|mips64|ppc|ppc64) TARGET_ARCH=$i
			;;
		release|debug|clean) BUILD_TYPE=$i
			;;
		ci) CI="ci"
			;;
		-j*) ENABLE_JOBS=1; BUILD_JOBS="${i#-j}"
			;;
		-v) BUILD_VERBOSE='VERBOSE=1' 
			;;
		--help|-h) usage
			;;
		*) echo "illegal option $i"
			usage
			;;
	esac
done

if [ $TARGET_ARCH != $HOST_ARCH ] || [ $TARGET_OS != $HOST_OS ]; then
	case $TARGET_ARCH in
		i386)
			if [ $HOST_ARCH = "amd64" ]; then
				BUILD_OPTION="-m32"
			else
				usage
			fi
			;;
		arm)
			if [ $TARGET_OS = "Linux" ]; then
				BUILD_OPTION="-march=armv7-a -mfpu=vfp3 -marm --target=arm-linux-gnueabihf -L/usr/lib/gcc-cross/arm-linux-gnueabihf/${GCC_VERSION} -L/usr/arm-linux-gnueabihf/lib -I/usr/arm-linux-gnueabihf/include -I/usr/arm-linux-gnueabihf/include/c++/${GCC_VERSION} -I/usr/arm-linux-gnueabihf/include/c++/${GCC_VERSION}/arm-linux-gnueabihf"
			else
				usage
			fi
			;;
		arm64)
			if [ $TARGET_OS = "Linux" ]; then
				BUILD_OPTION="--target=aarch64-linux-gnu -L/usr/lib/gcc-cross/aarch64-linux-gnu/${GCC_VERSION} -L/usr/aarch64-linux-gnu/lib -I/usr/aarch64-linux-gnu/include -I/usr/aarch64-linux-gnu/include/c++/${GCC_VERSION} -I/usr/aarch64-linux-gnu/include/c++/${GCC_VERSION}/aarch64-linux-gnu"
			else
				usage
			fi
			;;
		mips)
			if [ $TARGET_OS = "Linux" ]; then
				BUILD_OPTION="--target=mips-linux-gnu  -L/usr/lib/gcc-cross/mips-linux-gnu/${GCC_VERSION} -L/usr/mips-linux-gnu/lib -I/usr/mips-linux-gnu/include -I/usr/mips-linux-gnu/include/c++/${GCC_VERSION} -I/usr/mips-linux-gnu/include/c++/${GCC_VERSION}/mips-linux-gnu"
			else
				usage
			fi
			;;
		mips64)
			if [ $TARGET_OS = "Linux" ]; then
				BUILD_OPTION="--target=mips64-linux-gnuabi64 -L/usr/lib/gcc-cross/mips64-linux-gnuabi64/${GCC_VERSION} -L/usr/mips64-linux-gnuabi64/lib -I/usr/mips64-linux-gnuabi64/include -I/usr/mips64-linux-gnuabi64/include/c++/${GCC_VERSION} -I/usr/mips64-linux-gnuabi64/include/c++/${GCC_VERSION}/mips64-linux-gnuabi64"
			else
				usage
			fi
			;;
		ppc)
			if [ $TARGET_OS = "Linux" ]; then
                BUILD_OPTION="--target=powerpc-linux-gnu -L/usr/lib/gcc-cross/powerpc-linux-gnu/${GCC_VERSION} -L/usr/powerpc-linux-gnu/lib -I/usr/powerpc-linux-gnu/include -I/usr/powerpc-linux-gnu/include/c++/${GCC_VERSION} -I/usr/powerpc-linux-gnu/include/c++/${GCC_VERSION}/powerpc-linux-gnu"
			else
				usage
			fi
			;;
		ppc64)
			if [ $TARGET_OS = "Linux" ]; then
                BUILD_OPTION="--target=powerpc64-linux-gnu -L/usr/lib/gcc-cross/powerpc64-linux-gnu/${GCC_VERSION} -L/usr/powerpc64-linux-gnu/lib -I/usr/powerpc64-linux-gnu/include -I/usr/powerpc64-linux-gnu/include/c++/${GCC_VERSION} -I/usr/powerpc64-linux-gnu/include/c++/${GCC_VERSION}/powerpc64-linux-gnu"
			else
				usage
			fi
			;;
	esac
fi

inform()
{
	echo ""
	echo "submodule vender is not existed!"
	echo "you can execute the given command to init and update it."
	echo " \$ git submodule init"
	echo " \$ git submodule update"
	echo ""
	exit 1
}

if [ "$ENABLE_JOBS" = "1" -a "$BUILD_JOBS" = "" ]; then
	#get cpu core count 
	CPU_CORE=1
	case ${HOST_OS} in
		Darwin)
			CPU_CORE=$(sysctl hw.ncpu | awk '{print $2}')
			;;
		Linux)
			CPU_CORE=$(cat /proc/cpuinfo | grep processor | wc -l)
			;;
		Windows)
			CPU_CORE=$(echo $NUMBER_OF_PROCESSORS)
			;;
	esac
	echo "host machine has ${CPU_CORE} core"

	if [ "$CPU_CORE" = "1" ]; then
		BUILD_JOBS=""
	else
		# set build jobs with cpu core count
		BUILD_JOBS=${CPU_CORE}
	fi
fi

export BUILD_JOBS
export BUILD_VERBOSE

if [ -d vender ]; then 
	if [ -z $USE_VENDER_DIST ]; then
		cd vender
		sh build $BUILD_TYPE $TARGET_ARCH
		if [ $? != 0 ]; then
			exit 1
		fi
		cd ..
	fi
else
	inform
fi

cd fibjs
sh build $BUILD_TYPE $TARGET_ARCH $CI
if [ $? != 0 ]; then
	exit 1
fi
cd ..

cd fibjs/program
sh build $BUILD_TYPE $TARGET_ARCH $CI
if [ $? != 0 ]; then
	exit 1
fi
cd ../..

txtbld=$(tput bold)
bldred=${txtbld}$(tput setaf 1)
txtrst=$(tput sgr0)

printf "\n\n"
printf "\t${bldred} _______ _________ ______  _________ _______ ${txtrst}\n"
printf "\t${bldred}(  ____ \\\\\\__   __/(  ___ \\ \\__    _/(  ____ \\\\${txtrst}\n"
printf "\t${bldred}| (    \\/   ) (   | (   ) )   )  (  | (    \\/${txtrst}\n"
printf "\t${bldred}| (__       | |   | (__/ /    |  |  | (_____ ${txtrst}\n"
printf "\t${bldred}|  __)      | |   |  __ (     |  |  (_____  )${txtrst}\n"
printf "\t${bldred}| (         | |   | (  \\ \\    |  |        ) |${txtrst}\n"
printf "\t${bldred}| (_     ___) (___| (___) )/\\_)  )  /\\____) |${txtrst}\n"
printf "\t${bldred}(__/     \\_______/(______/ \\____/   \\_______)${txtrst}\n"
printf ""

printf "\\n\\tFIBJS has been successfully built.\\n\\n"

printf "\\tFor more information:\\n\\n"
printf "\\twebsite: http://fibjs.org\\n"
printf "\\trepository: https://github.com/fibjs\\n"
printf "\n\n"